package cn.wei.sdd.core.receiver;

import cn.wei.sdd.core.enums.OrderStatusEnum;
import cn.wei.sdd.core.mapper.OrdersMapper;
import cn.wei.sdd.core.pojo.entity.Orders;
import cn.wei.sdd.core.service.OrderService;
import cn.wei.sdd.rabbitutils.constant.MQConst;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: wei
 * @Date: 2022/02/20/20:41
 * 订单超时监听器
 */
@Slf4j
@Component
public class OrderReceiver {

    @Resource
    private OrdersMapper ordersMapper;

    @Resource
    private OrderService orderService;


    @RabbitListener(queues = MQConst.DELAY_QUEUE_ORDER)
    public void receiveOrder(Message message, Channel channel){
        //获取订单编号
        String no = new String(message.getBody());
        String orderNo = no.replace("\"", "");
        //记录日志
        log.info("接收延时队列，当前时间：{}，延迟队列编号：{}", LocalDateTime.now(),orderNo);
        //根据订单号查询顶订单
        Orders order = orderService.getOrderByOrderNo(orderNo);
        System.out.println(order);
        if (order!=null && order.getStatus().equals(OrderStatusEnum.Wait_Order.getStatus())){
            UpdateWrapper<Orders> ordersUpdateWrapper = new UpdateWrapper<>();
            ordersUpdateWrapper.eq("order_no",orderNo).set("status",OrderStatusEnum.overTime.getStatus());
            ordersMapper.update(null,ordersUpdateWrapper);
        }

    }
}
